package com.tl.commonlibrary.tool;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.support.annotation.Nullable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;
import com.tl.commonlibrary.AppConfig;
import com.tl.commonlibrary.R;
import com.tl.commonlibrary.network.NetConfig;

/**
 * Created by Administrator on 2017/6/14 0014.
 */

public class UIUtils {
    public static int getColor(Context context, int colorResId) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            return context.getColor(colorResId);
        }
        return context.getResources().getColor(colorResId);
    }

    public static SpannableString getColorSpannableString(String content, int start, int color) {
        return getColorSpannableString(content, start, content.length(), color);
    }

    public static SpannableString getColorSpannableString(String content, int start, int end, int color) {
        SpannableString spannableString = new SpannableString(content);
        spannableString.setSpan(new ForegroundColorSpan(color), start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        return spannableString;
    }

    /**
     * 浅色默认图片
     *
     * @param context
     * @param imageView
     * @param url
     */
    public static void setImage(Context context, ImageView imageView, String url) {
        if (TextUtils.isEmpty(url)) {
            imageView.setImageResource(R.drawable.ic_default);
        } else {
            Picasso.with(context).load(url).error(R.drawable.ic_default).placeholder(R.drawable.ic_default).into(imageView);
        }
    }

    /**
     * 深色默认图片
     *
     * @param context
     * @param imageView
     * @param url
     */
    public static void setImageDeep(Context context, ImageView imageView, String url) {
        if (!TextUtils.isEmpty(url)) {
            Picasso.with(context).load(url).error(R.drawable.ic_default_deep).placeholder(R.drawable.ic_default_deep).into(imageView);
        } else {
            imageView.setImageResource(R.drawable.ic_default_deep);
        }
    }

    public static boolean verifyPassword(String pwd) {
        if (TextUtils.isEmpty(pwd) || pwd.length() < 6 || pwd.length() > 16) {
            return false;
        }
        return true;
    }

    public static void showToast(int textResId) {
        showToast(textResId, -9);
    }

    public static void showToast(int textResId, int duration) {
        Toast toast = toastInstance(textResId, duration);
        if (toast == null) {
            return;
        }
        toast.show();
    }

    public static void showToast(String text) {
        showToast(text, -9);
    }

    public static void showToast(String text, int duration) {
        Toast toast = toastInstance(text, duration);
        if (toast == null) {
            return;
        }
        toast.show();
    }

    public static Toast toastInstance(String text) {
        return toastInstance(text, -9);
    }

    public static Toast toastInstance(int textResId) {
        return toastInstance(textResId, -9);
    }

    @Nullable
    public static Toast toastInstance(int textResId, int duration) {
        if (AppConfig.appContext == null) {
            return null;
        }
        return toastInstance(AppConfig.appContext.getString(textResId, duration));
    }

    @Nullable
    public static Toast toastInstance(String text, int duration) {
        if (TextUtils.isEmpty(text) || AppConfig.appContext == null) {
            return null;
        }
        Toast toast = new Toast(AppConfig.appContext);
        LayoutInflater inflate = (LayoutInflater)
                AppConfig.appContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = inflate.inflate(R.layout.toast, null);
        if (duration == -9) {
            if (text.length() > 15) {
                duration = Toast.LENGTH_LONG;
            } else {
                duration = Toast.LENGTH_SHORT;
            }
        }
        TextView textView = (TextView) v.findViewById(R.id.message);
        textView.setText(text);
        toast.setView(v);
        toast.setDuration(duration);
        return toast;
    }

    public static void startService(Context context) {
        DeviceUtil.dial(context, "4000002401");
    }

    /**
     * 启动QQ客服
     *
     * @param context
     */
    public static void onStartService(Context context) {
        try {
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_VIEW);
            Uri uri = Uri.parse(NetConfig.URL_QQ_SERVICE);
            intent.setData(uri);
            context.startActivity(intent);
        } catch (Exception e) {
            UIUtils.showToast(R.string.start_qq_service_error, Toast.LENGTH_LONG);
        }
    }

    /**
     * 启动指定QQ联系人
     *
     * @param context
     */
    public static void onStartService(Context context, String qq) {
        try {
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_VIEW);
            Uri uri = Uri.parse(String.format(NetConfig.URL_QQ, qq));
            intent.setData(uri);
            context.startActivity(intent);
        } catch (Exception e) {
            UIUtils.showToast(R.string.start_qq_service_error, Toast.LENGTH_LONG);
        }
    }

    public static final int negotiable() {
        return R.string.comm_price_negotiable;
    }

}
